package com.ruoyi.experiment.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.experiment.domain.ExperimentDesign;
import com.ruoyi.experiment.service.IExperimentDesignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.domain.ExperimentAnalysis;
import com.ruoyi.experiment.service.IExperimentAnalysisService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 数据分析Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/analysis")
public class ExperimentAnalysisController extends BaseController
{
    @Autowired
    private IExperimentAnalysisService experimentAnalysisService;

    @Autowired
    private IExperimentDesignService experimentDesignService;

    /**
     * 查询数据分析列表
     */
    @RequiresPermissions("Experiment:analysis:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentAnalysis experimentAnalysis)
    {
        startPage();
        List<ExperimentAnalysis> list = experimentAnalysisService.selectExperimentAnalysisList(experimentAnalysis);
        for (ExperimentAnalysis analysis : list) {
            if (analysis.getDesignId() != null) {
                ExperimentDesign design = experimentDesignService.selectExperimentDesignByDesignId(analysis.getDesignId());
                if (design != null) {
                    analysis.setDesignName(design.getDesignName());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出数据分析列表
     */
    @RequiresPermissions("Experiment:analysis:export")
    @Log(title = "数据分析", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentAnalysis experimentAnalysis)
    {
        List<ExperimentAnalysis> list = experimentAnalysisService.selectExperimentAnalysisList(experimentAnalysis);
        ExcelUtil<ExperimentAnalysis> util = new ExcelUtil<ExperimentAnalysis>(ExperimentAnalysis.class);
        util.exportExcel(response, list, "数据分析数据");
    }

    /**
     * 获取数据分析详细信息
     */
    @RequiresPermissions("Experiment:analysis:query")
    @GetMapping(value = "/{analysisId}")
    public AjaxResult getInfo(@PathVariable("analysisId") Long analysisId)
    {
        return success(experimentAnalysisService.selectExperimentAnalysisByAnalysisId(analysisId));
    }

    /**
     * 新增数据分析
     */
    @RequiresPermissions("Experiment:analysis:add")
    @Log(title = "数据分析", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentAnalysis experimentAnalysis)
    {
        return toAjax(experimentAnalysisService.insertExperimentAnalysis(experimentAnalysis));
    }

    /**
     * 修改数据分析
     */
    @RequiresPermissions("Experiment:analysis:edit")
    @Log(title = "数据分析", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentAnalysis experimentAnalysis)
    {
        return toAjax(experimentAnalysisService.updateExperimentAnalysis(experimentAnalysis));
    }

    /**
     * 删除数据分析
     */
    @RequiresPermissions("Experiment:analysis:remove")
    @Log(title = "数据分析", businessType = BusinessType.DELETE)
	@DeleteMapping("/{analysisIds}")
    public AjaxResult remove(@PathVariable Long[] analysisIds)
    {
        return toAjax(experimentAnalysisService.deleteExperimentAnalysisByAnalysisIds(analysisIds));
    }
}
